import {ConfirmationService, Message, MessageService} from 'primeng/api';
import {TranslateService} from '@ngx-translate/core';
import {MsgType} from './base.data';


export abstract class BaseComponent {

    locale: any = [];

    protected constructor(
        protected messageService: MessageService,
        protected confirmationService: ConfirmationService,
        protected translate: TranslateService) {
        this.locale = {
            firstDayOfWeek: 0,
            dayNames: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
            dayNamesShort: ['日', '一', '二', '三', '四', '五', '六'],
            dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
            monthNamesShort: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
            today: '今天',
            clear: '清除'
        };
    }


    public parseDate(date: string|Date, hideTime: boolean = false) {
        if (typeof date === "string") {
            return date;
        }
        const year: number = date.getFullYear();
        let month: string = (date.getMonth() + 1) + '';
        month = month.length === 1 ? `0${month}` : month;
        let day: string = date.getDate() + '';
        day = day.length === 1 ? `0${day}` : day;
        let hour: string = date.getHours() + '';
        hour = hour.length === 1 ? `0${hour}` : hour;
        let minute: string = date.getMinutes() + '';
        minute = minute.length === 1 ? `0${minute}` : minute;
        if (hideTime) {
            return `${year}/${month}/${day}`;
        }
        return `${year}/${month}/${day} ${hour}:${minute}`;
    }

    protected toast(msg: string, type: MsgType = MsgType.INFO) {
        switch (type) {
            case MsgType.SUCCESS:
                this.printSuccess(msg);
                break;
            case MsgType.ERROR:
                this.printError(msg);
                break;
            case MsgType.WARN:
                this.printWarn(msg);
                break;
            case MsgType.INFO:
                this.printInfo(msg);
                break;
            default:
                break;
        }
    }

    protected toastById(resId: string, type: MsgType = MsgType.INFO) {
        switch (type) {
            case MsgType.SUCCESS:
                this.printSuccess(this.translate.instant(resId));
                break;
            case MsgType.ERROR:
                this.printError(this.translate.instant(resId));
                break;
            case MsgType.WARN:
                this.printWarn(this.translate.instant(resId));
                break;
            case MsgType.INFO:
                this.printInfo(this.translate.instant(resId));
                break;
            default:
                break;
        }
    }

    private printError(msg: string) {
        this.messageService.clear();
        this.messageService.add(<Message>{
            key: 'sys_msg',
            severity: 'error',
            summary: this.translate.instant('commons.error'),
            detail: msg
        });
    }

    private printWarn(msg: string) {
        this.messageService.clear();
        this.messageService.add(<Message>{
            key: 'sys_msg',
            severity: 'warn',
            summary: this.translate.instant('commons.warn'),
            detail: msg
        });
    }

    private printSuccess(msg: string) {
        this.messageService.clear();
        this.messageService.add(<Message>{
            key: 'sys_msg',
            severity: 'success',
            summary: this.translate.instant('commons.success'),
            detail: msg
        });
    }

    private printInfo(msg: string) {
        this.messageService.clear();
        this.messageService.add(<Message>{
            key: 'sys_msg',
            severity: 'info',
            summary: this.translate.instant('commons.info'),
            detail: msg
        });
    }

    protected confirm(msgKey: string, accept: Function, reject: Function = null) {
        this.confirmationService.confirm({
            key: 'confirm',
            message: this.translate.instant(msgKey),
            header: this.translate.instant('commons.info'),
            acceptLabel: this.translate.instant('commons.ok'),
            rejectLabel: this.translate.instant('commons.cancel'),
            icon: 'pi pi-exclamation-triangle',
            accept: accept,
            reject: reject,
            rejectVisible: true
        });
    }

    protected alert(msgKey: string) {
        this.confirmationService.confirm({
            key: 'alert',
            message: this.translate.instant(msgKey),
            header: this.translate.instant('commons.warn'),
            acceptLabel: this.translate.instant('commons.ok'),
            icon: 'pi pi-exclamation-triangle',
            rejectVisible: false
        });
    }

}
